package org.code;

import java.util.HashMap;
import java.util.Map;

/**
 * Integer to English Words
 * 
 * <pre>
 * for example:
 * 	123 -> "One Hundred Twenty Three"
 * 	12345 -> "Twelve Thousand Three Hundred Forty Five"
 * 	1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
 * </pre>
 * 
 * @author zhujunbing
 * @date 2018年4月18日
 */
public class Lt273 {
	private static Map<Integer, String> base = new HashMap<Integer, String>(32);
	private static Map<Integer, String> map = new HashMap<Integer, String>(1000);

	static {
		base.put(0, "Zero");
		base.put(1, "One");
		base.put(2, "Two");
		base.put(3, "Three");
		base.put(4, "Four");
		base.put(5, "Five");
		base.put(6, "Six");
		base.put(7, "Seven");
		base.put(8, "Eight");
		base.put(9, "Nine");
		base.put(10, "Ten");
		base.put(11, "Eleven");
		base.put(12, "Twelve");
		base.put(13, "Thirteen");
		base.put(14, "Fourteen");
		base.put(15, "Fifteen");
		base.put(16, "Sixteen");
		base.put(17, "Seventeen");
		base.put(18, "Eighteen");
		base.put(19, "Nineteen");
		base.put(20, "Twenty");
		base.put(30, "Thirty");
		base.put(40, "Forty");
		base.put(50, "Fifty");
		base.put(60, "Sixty");
		base.put(70, "Seventy");
		base.put(80, "Eighty");
		base.put(90, "Ninety");
		base.put(100, "Hundred");
		base.put(1000, "Thousand");
		base.put(1000_000, "Million");
		base.put(1000_000_000, "Billion");
		map = new HashMap<Integer, String>();

		for (int i = 0; i < 1000; i++) {

			if (i < 100) {
				if (i <= 20) {
					map.put(i, base.get(i));
				} else {
					int j = i % 10;
					int k = i - j;
					map.put(i, base.get(k) + (j != 0 ? " " + base.get(j) : ""));
				}
			} else {
				int s = i % 100;
				int t = i / 100;
				map.put(i, map.get(t) + " Hundred" + (s != 0 ? " " + map.get(s) : ""));
			}
		}

	}

	public String numberToWords(int num) {

		if (num == 0) {
			return "Zero";
		}
		StringBuilder sb = new StringBuilder();
		
		boolean flag = false;
		int a = num % 1000;
		num = num / 1000;
		if (a != 0) {
			String str1 = getStr(a);
			sb.insert(0, str1);
			flag = true;
		}
		int b = num % 1000;
		num = num / 1000;
		if (b != 0) {
			String str2 = getStr(b);
			sb.insert(0, str2 + " Thousand"+(flag?" ":""));
			flag = true;
		}
		int c = num % 1000;
		num = num / 1000;
		if (c > 0) {
			String str3 = getStr(c);
			sb.insert(0, str3 + " Million"+(flag?" ":""));
			flag = true;
		}

		int d = num % 1000;
		if (d > 0) {
			String str4 = getStr(d);
			sb.insert(0, str4 + " Billion"+(flag?" ":""));
		}

		return sb.toString();
	}

	private String getStr(int a) {
		return map.get(a);
	}

	public static void main(String[] args) {
		System.out.println(new Lt273().numberToWords(1234567));



	}

}
